package com.group.neps.controller;

import com.group.common.utils.MyUtils;
import com.group.neps.entity.City;
import com.group.neps.entity.Location;
import com.group.neps.entity.RowAqiData;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

import java.io.*;
import java.util.ArrayList;

public class FeedbackDataViewController {
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn<RowAqiData, String> colUserName;
    @FXML
    private TableColumn<RowAqiData, String> colDate;
    @FXML
    private TableColumn<RowAqiData, String> colProvince;
    @FXML
    private TableColumn<RowAqiData, String> colCity;
    @FXML
    private TableColumn<RowAqiData, String> colRowAqiLevel;
    @FXML
    private TableColumn<RowAqiData, String> colRemark;
    @FXML
    private Button btnBack;

    //从文件中读取监督员提交的反馈数据
    private ArrayList<RowAqiData> feedbackDataList = MyUtils.readFileToArrayList("group/data/row_aqi_data_arraylist.dat");

    @FXML
    public void initialize() throws IOException, ClassNotFoundException {
        initializeTableColumns();
        loadDataToTable();
        //对反馈数据列表赋初值
        feedbackDataList =  MyUtils.readFileToArrayList("group/data/row_aqi_data_arraylist.dat");
    }
    //初始化表格列
    private void initializeTableColumns() {
        // 反馈者姓名列（来自NEPSUser）
        colUserName.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getUser().getUsername()));

        // 所在省列（来自Location）
        colProvince.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getLocation().getProvince().getName()));

        // 所在市列（来自Location）
        colCity.setCellValueFactory(cellData -> {
            Location location = cellData.getValue().getLocation();
            City city = (location != null) ? location.getCity() : null;
            String name = (city != null) ? city.getName() : "";
            return new SimpleStringProperty(name);
        });

        // 预估污染等级列（来自RowAqiLevel）
        colRowAqiLevel.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getAqiLevel().toString()));

        // 反馈日期列
        colDate.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getTime().toString()));

        //反馈详情列
        colRemark.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getRemark()));
    }

    private void loadDataToTable() throws IOException, ClassNotFoundException {
        // 读取HashMap数据
        feedbackDataList = MyUtils.readFileToArrayList("group/data/row_aqi_data_arraylist.dat");

        // 转换为ObservableList
        ObservableList<RowAqiData> tableData = FXCollections.observableArrayList(feedbackDataList);

        // 设置到TableView
        tableView.setItems(tableData);
    }

    @FXML
    private void back() {
        //返回到监督员提交页面
        MyUtils.showView((Stage) btnBack.getScene().getWindow(), "group/neps/view/FeedbackProvideView.fxml");
    }
}
